package com.cskaoyan.service;

import com.cskaoyan.bean.Keyword;
import com.cskaoyan.bean.KeywordExample;
import com.cskaoyan.bean.KeywordlistVO;
import com.cskaoyan.mapper.KeywordMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class KeywordServiceImpl implements KeywordService {
    @Autowired
    KeywordMapper keywordMapper;
    @Override
    public KeywordlistVO<Keyword> queryKeywordList(Integer page, Integer limit, String sort, String order, String keyword, String url) {
        PageHelper.startPage(page,limit);
        KeywordlistVO<Keyword> keywordvo = new KeywordlistVO<>();
        KeywordExample keywordExample = new KeywordExample();
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        if (keyword != null) {
            criteria.andKeywordEqualTo(keyword);
        }
        if(url!=null){
            criteria.andUrlEqualTo(url);
        }
        List<Keyword> keywordList = keywordMapper.selectByExample(keywordExample);
        PageInfo pageInfo = new PageInfo(keywordList);
        long total = pageInfo.getTotal();
        keywordvo.setItems(keywordList);
        keywordvo.setTotal(total);
        return keywordvo;
    }

    @Override
    public Keyword createKeyword(Keyword keyword) {
        Date date=new Date();
        keyword.setDeleted(false);
        keyword.setAddTime(date);
        keyword.setUpdateTime(date);
        keywordMapper.insert(keyword);
        return keyword;
    }

    /**
     * 更新
     * @param keyword
     * @return
     */
    @Override
    public Keyword updateKeyword(Keyword keyword) {
        keyword.setUpdateTime(new Date());
        keywordMapper.updateByPrimaryKey(keyword);
        return keyword;
    }

    /**
     * 删除关键词
     * @param keyword
     * @return
     */
    @Override
    public int deleteKeyword(Keyword keyword) {
        int delete=keywordMapper.deleteByPrimaryKey(keyword.getId());
        return delete;
    }
}
